var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var app = {
	srcPath: 'src/',
	devPath: 'build/',
	prdPath: 'dist/'
}

gulp.task('lib', function() {
	gulp.src('bower_components/**/*')
		.pipe($.plumber())
		.pipe(gulp.dest(app.devPath + 'lib'))
		.pipe(gulp.dest(app.prdPath + 'lib'))
	gulp.src(app.srcPath + 'plug/**/*')
		.pipe($.plumber())
		.pipe(gulp.dest(app.devPath + 'plug'))
		.pipe(gulp.dest(app.prdPath + 'plug'))
})

gulp.task('html', function() {
	gulp.src(app.srcPath + '**/*.html')
		.pipe($.plumber())
		.pipe(gulp.dest(app.devPath))
		.pipe(gulp.dest(app.prdPath))
})

gulp.task('json', function() {
	gulp.src(app.srcPath + '**/*.json')
		.pipe($.plumber())
		.pipe(gulp.dest(app.devPath))
		.pipe(gulp.dest(app.prdPath))
})

gulp.task('pdf', function() {
	gulp.src(app.srcPath + '**/*.pdf')
		.pipe($.plumber())
		.pipe(gulp.dest(app.devPath))
		.pipe(gulp.dest(app.prdPath))
})

gulp.task('less', function() {
	gulp.src(app.srcPath + 'style/index.less')
		.pipe($.plumber())
		.pipe($.less())
		.pipe(gulp.dest(app.devPath + 'css'))
		.pipe($.cssmin())
		.pipe(gulp.dest(app.prdPath + 'css'))
})

gulp.task('js', function() {
	gulp.src(app.srcPath + 'script/**/*.js')
		.pipe($.plumber())
		.pipe($.concat('index.js'))
		.pipe(gulp.dest(app.devPath + 'js'))
		.pipe($.uglify({
			preserveComments:"some",
			compress: false,
			mangle: true, 
		}))
		.pipe(gulp.dest(app.prdPath + 'js'))
})

gulp.task('image', function() {
	gulp.src(app.srcPath + 'image/**/*')
		.pipe($.plumber())
		.pipe(gulp.dest(app.devPath + 'image'))
		.pipe($.imagemin())
		.pipe(gulp.dest(app.prdPath + 'image'))
})

gulp.task('staticImage', function() {
	gulp.src(app.srcPath + 'static/**/*')
		.pipe($.plumber())
		.pipe(gulp.dest(app.devPath + 'static'))
		.pipe($.imagemin())
		.pipe(gulp.dest(app.prdPath + 'static'))
})



gulp.task('clean', function() {
	gulp.src([app.devPath, app.prdPath])
		.pipe($.clean())
})

gulp.task('bulid', ['lib', 'image','staticImage', 'less', 'js', 'html', 'pdf', 'json'])

gulp.task('start', ['bulid'], function() {
	gulp.watch('bower_components/**/*', ['lib']);
	gulp.watch(app.srcPath + '**/*.html', ['html']);
	gulp.watch(app.srcPath + '**/*.pdf', ['pdf']);
	gulp.watch(app.srcPath + 'image/**/*', ['image']);
	gulp.watch(app.srcPath + 'static/**/*', ['staticImage']);
	gulp.watch(app.srcPath + 'script/**/*.js', ['js']);
	gulp.watch(app.srcPath + 'style/**/*.less', ['less']);
	gulp.watch(app.srcPath + '**/*.json', ['json']);
})

gulp.task('default', ['start'])